package me.zhanshi123.invitelink;

import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.util.List;
import java.util.Map;

public class Commands implements CommandExecutor {
    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (!(sender instanceof Player)) {
            return true;
        }
        Player player = (Player) sender;
        String name = player.getName();
        sender.sendMessage("§7你的分享链接: §e" + Main.getConfigManager().getUrl() + "?user=" + name);
        sender.sendMessage("§7邀请了一定数量了以后再输入该命令可以领取奖励哦");
        int amount = Main.getPluginDatabase().getSharedAmount(name);
        Map<Integer, List<String>> award = Main.getConfigManager().getReachedAwards(amount);
        award.entrySet().forEach(entry -> {
            if (!Main.getPluginDatabase().isAwarded(name, entry.getKey())) {
                Main.getPluginDatabase().claimAward(name,entry.getKey());
                entry.getValue().forEach(cmd -> Bukkit.dispatchCommand(Bukkit.getConsoleSender(), cmd.replace("{player}", name)));
                sender.sendMessage("§e你成功领取了分享§c"+entry.getKey()+"§e次的奖励");
            }
        });
        return true;
    }
}
